package com.jinlejia.erp.ui.message;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.freedom.yefeng.yfrecyclerview.YfListMode;
import com.jinlejia.erp.R;
import com.jinlejia.erp.adapter.MessageAdapter;
import com.jinlejia.erp.base.ApiConfig;
import com.jinlejia.erp.base.MapCallBack;
import com.jinlejia.erp.databinding.FragmentMessageListBinding;
import com.jinlejia.erp.utils.DataFilter;
import com.zhy.http.okhttp.OkHttpUtils;

import java.util.ArrayList;
import java.util.Map;

import okhttp3.Call;

/**
 * Created by Administrator on 2017/9/12 0012.
 */

public class MessageListFragment extends Fragment implements  SwipeRefreshLayout.OnRefreshListener{


    private MessageAdapter adapter,adapter2;
    private ArrayList<Map<String, Object>> dataList = new ArrayList<>();
    private ArrayList<Map<String, Object>> dataList2 = new ArrayList<>();
    private View view;
    private boolean showMore = false;

    private FragmentMessageListBinding binding;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        if (view == null) {
            view = inflater.inflate(R.layout.fragment_message_list, null);
            binding = DataBindingUtil.bind(view);
            initView();
            initEvent();
        }
        // 缓存的rootView需要判断是否已经被加过parent，
        // 如果有parent需要从parent删除，要不然会发生这个rootview已经有parent的错误。
        ViewGroup parent = (ViewGroup) view.getParent();
        if (parent != null) {
            parent.removeView(view);
        }
        binding = DataBindingUtil.bind(view);
        return view;
    }
    @Override
    public void onStart() {
        super.onStart();
        getData();
    }

    /**
     * 初始化视图
     */
    private void initView() {
        binding.refreshLayout.setColorSchemeResources(R.color.yellow, R.color.light_yellow);
        binding.refreshLayout.setOnRefreshListener(this);

        binding.messageRecycleView.setHasFixedSize(true);
        binding.messageRecycleView.setLayoutManager(new LinearLayoutManager(getActivity()));
        binding.messageRecycleView2.setHasFixedSize(true);
        binding.messageRecycleView2.setLayoutManager(new LinearLayoutManager(getActivity()));

        adapter = new MessageAdapter(dataList);
        adapter2 = new MessageAdapter(dataList2);
        binding.messageRecycleView.setAdapter(adapter);
        binding.messageRecycleView2.setAdapter(adapter2);
        adapter.changeMode(YfListMode.MODE_LOADING);
    }

    private void initEvent() {
        adapter.setOnItemClickListener((view, o) -> {
            Activity activity = getActivity();
            if (activity==null)return;
            Intent intent = new Intent();
            Map<String,Object> map = ((Map<String, Object>) o);
            String serviceType = map.get("serviceType").toString();
            if ("zuijinfangke".equals(serviceType)){
                //intent.setClass(activity,VisitorMainActivity.class);
            }else {
                intent.setClass(activity,MessageDetailsActivity.class);
                intent.putExtra("serviceType", serviceType);
                intent.putExtra("time","2021");
                intent.putExtra("title", map.get("title").toString());
            }
            startActivity(intent);
        });

        adapter2.setOnItemClickListener((view, o) -> {
            Activity activity = getActivity();
            if (activity==null)return;
            Intent intent = new Intent();
            Map<String,Object> map = ((Map<String, Object>) o);
            String serviceType = map.get("serviceType").toString();
            if ("zuijinfangke".equals(serviceType)){
                //intent.setClass(activity,VisitorMainActivity.class);
            }else {
                intent.setClass(activity,MessageDetailsActivity.class);
                intent.putExtra("serviceType", serviceType);
                intent.putExtra("time","2020");
                intent.putExtra("title", map.get("title").toString());
            }
            startActivity(intent);
        });

        binding.searchMessage.setOnClickListener(view -> {
            Intent intent = new Intent(getActivity(), SearchMessageActivity.class);
            startActivity(intent);
        });

        binding.layoutTimeMessageTestTips.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (showMore){
                    binding.iMTimeMessageTestTips.setImageResource(R.mipmap.ic_message_arrow_down);
                    binding.messageRecycleView2.setVisibility(View.GONE);
                    showMore=false;
                }else{
                    binding.iMTimeMessageTestTips.setImageResource(R.mipmap.ic_message_arrow_up);
                    binding.messageRecycleView2.setVisibility(View.VISIBLE);
                    showMore=true;
                }
            }
        });
    }


    /**
     * 获取数据
     */
    public void getData() {
        OkHttpUtils
                .get()
                .url(ApiConfig.MESSAGE_LIST.concat("2021"))
                .build()
                .execute(new MapCallBack() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onResponse(Map<String, Object> response, int id) {
                        if (DataFilter.filter(getActivity(), response, adapter)) {
                            ArrayList<Map<String, Object>> list = JSON.parseObject(response.get("data").toString(), new TypeReference<ArrayList<Map<String, Object>>>() {
                            });
                            adapter.setData(list);
                        }
                        if (binding.refreshLayout.isRefreshing()) {
                            binding.refreshLayout.setRefreshing(false);
                        }
                    }
                });


        OkHttpUtils
                .get()
                .url(ApiConfig.MESSAGE_LIST.concat("2020"))
                .build()
                .execute(new MapCallBack() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onResponse(Map<String, Object> response, int id) {
                        if (DataFilter.filter(getActivity(), response, adapter2)) {
                            ArrayList<Map<String, Object>> list = JSON.parseObject(response.get("data").toString(), new TypeReference<ArrayList<Map<String, Object>>>() {
                            });
                            adapter2.setData(list);
                        }
                        if (binding.refreshLayout.isRefreshing()) {
                            binding.refreshLayout.setRefreshing(false);
                        }
                    }
                });
    }

    @Override
    public void onRefresh() {
        getData();
    }

}
